package com.han.gift.controller;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.han.gift.common.util.AjaxResult;
import com.han.gift.common.util.PageList;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.han.gift.common.util.StrUtils;
import com.han.gift.entity.Config;
import com.han.gift.entity.TenantType;
import com.han.gift.query.ConfigQuery;
import com.han.gift.service.ConfigService;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;

@RestController
@RequestMapping("config")
public class ConfigController {

    //@Autowired
    //public ${table.serviceName} configService;

    /**
     * 服务对象
     */
    @Autowired
    private ConfigService configService;


    /**
     * 保存和修改公用的
     *
     * @param Config 传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Config config) {
        try {
            if (config.getConfigId() != null)
                configService.updateById(config);
            else
                configService.save(config);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("保存对象失败！" + e.getMessage());
        }
    }

    /**
     * 删除对象信息
     *
     * @param id
     * @return
     */
    @DeleteMapping(value = "/{id}")
    public AjaxResult delete(@PathVariable("id") Long id) {
        try {

            configService.removeById(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("删除对象失败！" + e.getMessage());
        }
    }

    //获取用户
    @GetMapping("/{id}")
    public AjaxResult get(@PathVariable("id") Long id) {
        try {

            Config config = configService.getById(id);
            return AjaxResult.me().setResultObj(config);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("获取一个失败！" + e.getMessage());
        }
    }


    /**
     * 查看所有的员工信息
     *
     * @return
     */
    @GetMapping
    public AjaxResult list() {

        try {
            List<Config> list = configService.list(null);
            return AjaxResult.me().setResultObj(list);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("获取所有失败！" + e.getMessage());
        }
    }


    /**
     * 分页查询数据
     *
     * @param query 查询对象
     * @return PageList 分页对象
     */

    /**
     * 分页查询数据
     *
     * @param query 查询对象
     * @return PageList 分页对象
     */
    @PostMapping("/list")
    public AjaxResult json(@RequestBody ConfigQuery query) {
        Page<Config> page = new Page<>(query.getPage(), query.getRows());
        String keyword = query.getKeyword();

//
            LambdaQueryWrapper<TenantType> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.like(TenantType::getDescription,keyword)
                    .or().like(TenantType::getName, keyword);


           page= configService.page(page);

        PageList pageList = new PageList<>(page.getTotal(), page.getRecords());
        return AjaxResult.me().setResultObj(pageList);
    }
}

